<template>
  <div class="reflex-maps">
    <div class="select" @click="changeVisible">
      <div class="left">
        <img width="16" height="16" style="cursor: pointer" :src="activeInfo?.address" />
        <span class="name" @click.stop="handleClick" v-if="!showInput">{{ activeInfo?.name }}</span>
        <el-input
          v-model="activeInfo.name"
          :autofocus="true"
          @click.stop="null"
          class="name"
          v-else
          style="width: 100px"
          @blur="handleBlur"
          @input="handleFilter"
        />
      </div>
      <SvgIcon
        ref="buttonRef"
        v-click-outside="onClickOutside"
        @click="changeVisible"
        icon-class="ic_down"
      />
    </div>
    <el-popover
      placement="bottom"
      :virtual-ref="buttonRef"
      :offset="20"
      popper-class="reflex-map_popover"
      effect="dark"
      :width="264"
      trigger="click"
      ref="popoverRef"
      :visible="visible"
    >
      <template #default>
        <div class="title">
          <span class="name">HDR贴图</span>
          <!-- .ktx2 临时 -->
          <UploadImage accept=".hdr,.ktx2" :type="props.type" @success="search" />
        </div>
        <div class="list">
          <div
            class="item"
            :class="activeInfo?.address === item.address ? 'active' : ''"
            v-for="item of fileList"
            :key="item.receiveId"
            @click="handleSelect(item)"
          >
            <div class="icon">
              <img src="@/assets/png/ic_hdr.png" />
              <img src="@/assets/png/ic_remove.png" @click.stop="handleRemove(item.receiveId)" />
            </div>
            <el-popover
              effect="dark"
              :width="50"
              popper-class="reflex-map_popover_name"
              placement="bottom"
              trigger="hover"
              :content="item.name"
            >
              <template #reference>
                <img :title="item.name" :src="item.address" />
              </template>
            </el-popover>
          </div>
        </div>
      </template>
    </el-popover>
  </div>
</template>

<script lang="ts" setup>
import { ref, unref } from 'vue';
import { ClickOutside as vClickOutside, ElMessageBox } from 'element-plus';
import UploadImage from '../upload-image/index.vue';
import { Files } from '@/api/common/types';
import { getFileList, deleteFile } from '@/api/common';
import { getResPath } from '@/utils/cimup';

interface IProps {
  type: string;
  modelValue: string;
}

const props = withDefaults(defineProps<IProps>(), {
  type: 'reflection',
});
const emits = defineEmits<{
  (e: 'update:modelValue', value: string): void;
}>();

const { type } = toRefs(props);

const buttonRef = ref();
const popoverRef = ref();
const pageInfo = reactive({ pageSize: 9999, pageNum: 1 });
const fileList = ref<Files.Item[]>([]);
const sourceList = ref<Files.Item[]>([]);
const activeInfo = ref<Files.Item>({
  name: '',
  address: '',
  receiveId: '',
});
const count = ref(0);
const visible = ref(false);
const feature = ref('public');
const showInput = ref(false);
const loading = ref(false);

const onClickOutside = () => {
  unref(popoverRef).popperRef?.delayHide?.();
};
const changeVisible = () => {
  visible.value = !visible.value;
};
const handleClick = () => {
  showInput.value = true;
};
const handleBlur = () => {
  showInput.value = false;
  if (!activeInfo.value?.name) {
    activeInfo.value = {
      ...fileList.value.find((x) => x.receiveId === activeInfo.value?.receiveId)!,
    };
  }
};
const handleFilter = () => {
  fileList.value = sourceList.value?.filter((x) => x.name.includes(activeInfo.value?.name));
};
const search = async () => {
  pageInfo.pageNum = 1;
  await getList();
};
const getList = async () => {
  loading.value = true;
  const res = await getFileList(pageInfo, { type: type.value, feature: feature.value }).finally(
    () => (loading.value = false)
  );
  // fileList.value = res.list;
  fileList.value = [
    {
      address: 'comm/images/3ar1hdrw.ajn.ktx2',
      name: 'city_morning.ktx2',
      receiveId: '6cba62e4-c33a-486b-8e68-63577fa86a22',
    },
  ]; // 临时
  sourceList.value = res.list?.map((x) => {
    return { ...x, address: getResPath(x.address) };
  });
  count.value = res.count;
};
const handleSelect = (item: Files.Item) => {
  activeInfo.value = { ...item };
};
const handleRemove = async (receiveId: string) => {
  await ElMessageBox.confirm('请确定要删除吗？', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  });
  await deleteFile({ receiveId });
  search();
};
const init = () => {
  if (props.modelValue) {
    activeInfo.value = { ...fileList.value.find((x) => x.address === props.modelValue)! };
  } else {
    activeInfo.value = { ...fileList.value[0] };
  }
};
onMounted(async () => {
  await search();
  init();
});
watch(
  () => activeInfo.value.address,
  (val) => {
    emits('update:modelValue', val);
  }
);
</script>

<style lang="less" scoped>
.reflex-maps {
  width: 100%;
  .row {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .select {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 4px;
    height: 24px;
    line-height: 24px;
    background-color: #303030;
    padding: 4px 0px;
    padding-right: 8px;
    background: #303030;
    .left {
      display: flex;
      align-items: center;
      .name {
        font-size: 12px;
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        margin-left: 8px;
      }
    }
  }
}
.reflex-map_popover {
  .title {
    background: #3d3d3d;
    border-radius: 4px;
    padding: 4px 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .name {
      font-size: 12px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      color: #ffffff;
    }
  }
  .list {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 14px;
    margin-top: 14px;
    .item {
      width: 48px;
      height: 48px;
      border-radius: 4px;
      position: relative;
      &.active {
        border: 1px solid #0560fd;
      }
      .icon {
        position: absolute;
        top: 5px;
        left: 5px;
        right: 5px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        img:first-child {
          width: 9px;
          height: 7px;
        }
        img:last-child {
          width: 10px;
          height: 10px;
          cursor: pointer;
        }
      }
      img {
        width: 100%;
        height: 100%;
        border-radius: 4px;
      }
      .name {
        background: #1d1d1d;
        box-shadow: 0px 9px 28px 8px #1d1d1d, 0px 6px 16px 0px #1d1d1d, 0px 3px 6px -4px #1d1d1d;
        border-radius: 2px;
        margin-top: 7px;
        text-align: center;
      }
      .remove {
        background: #0560fd;
        border-radius: 0px 0px 2px 2px;
        opacity: 0.68;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        text-align: center;
        display: none;
        img {
          width: 10px;
          height: 10px;
        }
      }
    }
  }
}
::v-deep(.reflex-map_popover_name) {
  min-width: 50px;
}
</style>
